package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.NoticeDto;
import com.huike.entity.Notice;
import com.huike.entity.SysUser;
import com.huike.mapper.NoticeMapper;
import com.huike.mapper.SysUserMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.NoticeService;
import com.huike.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 通知公告表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PageResult findAll(NoticeDto dto) {
        IPage page = new Page(dto.getPage(), dto.getPageSize());
        if (page.getSize() > 20) {
            page.setSize(20);
        }
        noticeMapper.selectPage(page,null);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public List<Notice> check(Long currentId, String status) {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getNoticeId,currentId);
        lambdaQueryWrapper.eq(Notice::getStatus,status);
        return noticeMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Result getInfo(Long id) {
        Notice notice = noticeMapper.selectById(id);
        return Result.success(notice);
    }

    @Override
    public void updateInfo(NoticeDto dto) {
        Notice notice=new Notice();
        BeanUtils.copyProperties(dto,notice);
        notice.setMag("2");
        noticeMapper.updateById(notice);
    }


}
